#import "MainScene.h"
#import "SimpleAudioEngine.h"

#define kComponentWidth 50
#define kComponentHeight 50
#define kComponentSpacing 10

@interface MainScene()

@property(nonatomic,strong) CCButton *clickButton;
@property(nonatomic,assign) int stopNumber;

@end


@implementation MainScene

@synthesize clickButton;
@synthesize stopNumber;

- (id)init
{
    // Apple recommend assigning self with supers return value
    self = [super init];
    CGSize size = [[CCDirector sharedDirector] viewSize];
    [self setColor:[CCColor greenColor]];
    // Background
    CCSprite *sprite = [CCSprite spriteWithImageNamed:@"vdo_picker"];
    sprite.position = (CGPoint){size.width * 0.5, 120.0f};
    [self addChild:sprite];
    /*
    // The standard Hello World text
    CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"ArialMT" fontSize:16];
    label.positionType = CCPositionTypeNormalized;
    label.anchorPoint = ccp(0.5, 0);
    label.position = (CGPoint){0.5, 40.0/(size.height)};
    [self addChild:label];
     */
    
    //[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"glu.mp3"];
    
    pickerView = [CCPickerView node];
    pickerView.position = ccp(size.width*0.5,120.0f);
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [self addChild:pickerView];
    
    self.clickButton =
    [CCButton buttonWithTitle:@"PLAY"
                  spriteFrame:[CCSpriteFrame frameWithImageNamed:@"StartBtn_N"]
       highlightedSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"StartBtn_H"]
          disabledSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"StartBtn_H"]];
    clickButton.anchorPoint = ccp(0.5, 0);
    clickButton.position = (CGPoint){size.width * 0.5, 10.0f};
    [clickButton setTarget:self selector:@selector(spinPickerEase)];
    [self addChild:clickButton];
    return self;
}

-(void)spinPickerEase {
    self.stopNumber = 0;
    [self.clickButton setEnabled:NO];
    int stopRow0 = arc4random() % 12;
    int stopRow1 = arc4random() % 12;
    int stopRow2 = arc4random() % 12;
    int stopRow3 = arc4random() % 12;
    NSLog(@"%d,%d,%d,%d",stopRow0,stopRow1,stopRow2,stopRow3);
    
    // Change the easeRate, speed, repeat and stopRow for the desired spin effect.
    // Seems like there is a bug with EaseInOut so use an integer value for easeRate.
    [pickerView spinComponent:0 speed:20 easeRate:1 repeat:2 stopRow:stopRow0];
    [pickerView spinComponent:1 speed:20 easeRate:1 repeat:2 stopRow:stopRow1];
    [pickerView spinComponent:2 speed:20 easeRate:1 repeat:2 stopRow:stopRow2];
    [pickerView spinComponent:3 speed:20 easeRate:1 repeat:2 stopRow:stopRow3];
}

#pragma mark - CCPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(CCPickerView *)pickerView {
    return 4;
}

- (NSInteger)pickerView:(CCPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 12;
}

#pragma mark - CCPickerViewDelegate
- (CGFloat)pickerView:(CCPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return kComponentHeight;
}

- (CGFloat)pickerView:(CCPickerView *)pickerView widthForComponent:(NSInteger)component {
    return kComponentWidth;
}

- (NSString *)pickerView:(CCPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return @"Not used";
}

- (CCNode *)pickerView:(CCPickerView *)pickerView nodeForRow:(NSInteger)row forComponent:(NSInteger)component reusingNode:(CCNode *)node {
    
    CCSprite* sprite = [CCSprite spriteWithImageNamed:[NSString stringWithFormat:@"icon%d.png", row+1]];
    [sprite setScale:0.4];
    return sprite;
}

- (void)pickerView:(CCPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSString*)component {
    CCLOG(@"didSelect row = %d, component = %@", row, component);
}

- (CGFloat)spaceBetweenComponents:(CCPickerView *)pickerView {
    return kComponentSpacing;
}

- (CGSize)sizeOfPickerView:(CCPickerView *)pickerView {
    CGSize size = m_slotwindow.boundingBox.size;
    return size;
}

- (CCNode *)overlayImage:(CCPickerView *)pickerView {
    return [CCSprite spriteWithImageNamed:@"slot_window.png"];
}

- (void)onDoneSpinning:(CCPickerView *)pickerView component:(NSString*)component {
    switch ([component intValue]) {
        case 0: {
            break;
        }
        case 1: {
            break;
        }
        case 2: {
            
            break;
        }
        default:
            break;
    }
    self.stopNumber++;
    if(self.stopNumber >= 3){
        self.clickButton.enabled = YES;
    }
    NSLog(@"Component %@ stopped spinning.", component);
}

@end
